package com.ad_hccm.controller;

import com.ad_hccm.domain.User;
import com.ad_hccm.exception.BusinessException;
import com.ad_hccm.interceptor.TokenUtil;
import com.ad_hccm.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * TODO 医生Controller
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    @Autowired
    TokenUtil tokenUtil;

    @PutMapping
    public Result update(@RequestBody User domain) {
        boolean flag = service.update(domain);
        String msg = flag ? "修改成功" : "数据修改失败，请重试";
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR , flag , msg);
    }

    @GetMapping
    public Result getByAccount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
//        if(id.length()!=18) throw new BusinessException(Code.GET_ERR, "请重试输入18位的身份证");
        User domain = service.getByAccount(userId);
        Integer code = domain != null ? Code.GET_OK : Code.GET_ERR;
        String msg = domain != null ? "查询成功" : "没有该信息";
        return new Result(code , domain , msg);
    }

}
